import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/components/refresh_header.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/model/exam_category.dart';
import 'package:kxy_flutter_app/r.dart';
import 'package:kxy_flutter_app/view/root/view_model/app_info_model.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'widget/cate_exam_panel_widget.dart';

class ExaminationView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new ExaminationViewState();
  }
}

class ExaminationViewState extends State<ExaminationView> with AutomaticKeepAliveClientMixin {
  List<ExamCategory> cateList;
  Future loadFuture;
  RefreshController refreshController = new RefreshController();

  @override
  void initState() {
    loadFuture = loadCateList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        title: Consumer<AppInfoModel>(
          builder: (cxt, vm, child) {
            return Text(vm.examinationTitle);
          },
        ),
        centerTitle: true,
        leading: Consumer<AppInfoModel>(
          builder: (cxt, vm, child) {
            return Container(
              margin: EdgeInsets.fromLTRB(10, 10, 10, 10),
              child: vm.logoUrl.isEmpty
                  ? Image.asset(R.assetsCommonLogo)
                  : CachedNetworkImage(
                      imageUrl: vm.logoUrl,
                      fit: BoxFit.fitHeight,
                    ),
            );
          },
        ),
      ),
      body: FutureBuilder(
        future: loadFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return body();
          }
          return Container();
        },
      ),
    );
  }

  Widget body() {
    var examList = cateList.where((x) => x.type == 1).toList();
    var testList = cateList.where((x) => x.type == 2).toList()..add(new ExamCategory(id: 'zyts', name: '专业提升', type: 3));

    var listview = ListView(
      padding: EdgeInsets.all(10),
      children: [
        if (examList.length > 0)
          Container(
            margin: EdgeInsets.only(bottom: 20),
            child: CateExamPanelWidget(
              title: '我要考试',
              subtitle: '是时候展现真正的实力了',
              imgName: R.assetsExamExamHead,
              cateList: examList,
            ),
          ),
        CateExamPanelWidget(
          title: '我要答题',
          subtitle: '多种练习，轻松掌握。',
          imgName: R.assetsExamTestHead,
          cateList: testList,
        )
      ],
    );
    return SmartRefresher(
      controller: refreshController,
      enablePullDown: true,
      enablePullUp: false,
      header: RefreshHeader(),
      onRefresh: () async {
        await loadCateList();
        refreshController.refreshCompleted();
      },
      child: listview,
    );
  }

  @override
  bool get wantKeepAlive => true;

  Future loadCateList() async {
    var res = await Http.instance.post('/exam-app/exam/cate/list');
    if (res.code == 200) {
      setState(() {
        cateList = (res.data as List).map((item) => ExamCategory.fromJson(item)).toList();
      });
    } else {
      cateList = new List<ExamCategory>();
    }
  }
}
